/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ventanaPrincipal.java
 *
 * Created on 13/04/2011, 09:22:04 PM
 */

package practica2_minimax;

import java.awt.BorderLayout;
import java.awt.event.MouseEvent;
import javax.swing.ImageIcon;
import javax.swing.JLabel;

/**
 *
 * @author William
 */
public class ventanaPrincipal extends javax.swing.JFrame {

    public panelImg panel;
    public JLabel lbl1 = new JLabel();
    public JLabel lbl2 = new JLabel();
    public JLabel lbl3 = new JLabel();
    public JLabel lbl4 = new JLabel();
    public JLabel lbl5 = new JLabel();
    public JLabel lbl6 = new JLabel();
    public JLabel lbl7 = new JLabel();
    public JLabel lbl8 = new JLabel();
    public JLabel lbl9 = new JLabel();
    public int turno, profundidad;
    public int[] posiciones = {-1,-1,-1,-1,-1,-1,-1,-1,-1};
    public minimax mm;
    boolean final_move;
    /** Creates new form ventanaPrincipal */
    public ventanaPrincipal() {
        initComponents();
        turno = -1; //para jugador O => 0; jugador X => 1; no iniciado = -1; finalizado = 2
        panel = new panelImg();
        panel.setLayout(null);
        this.add(panel, BorderLayout.CENTER);
        this.pack();
        agregaLabels();
        mm = new minimax();
        mm.setTablero(posiciones);
        final_move = false;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        lblTitleJugador1 = new javax.swing.JLabel();
        lblJugador1 = new javax.swing.JLabel();
        lblTitleJugador2 = new javax.swing.JLabel();
        lblJugador2 = new javax.swing.JLabel();
        btnIniciarJuego = new javax.swing.JButton();
        rbtnPrincipiante = new javax.swing.JRadioButton();
        rbtnIntermedio = new javax.swing.JRadioButton();
        rbtnAvanzado = new javax.swing.JRadioButton();
        lblGanador = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Tic Tac Toe - Ventana Principal");
        setResizable(false);

        lblTitleJugador1.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        lblTitleJugador1.setText("Humano - O");

        lblTitleJugador2.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        lblTitleJugador2.setText("Computadora - X");

        btnIniciarJuego.setText("Iniciar Juego");
        btnIniciarJuego.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnIniciarJuegoActionPerformed(evt);
            }
        });

        rbtnPrincipiante.setSelected(true);
        rbtnPrincipiante.setText("Nivel Principiante");
        rbtnPrincipiante.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                rbtnPrincipianteActionPerformed(evt);
            }
        });

        rbtnIntermedio.setText("Nivel Intermedio");
        rbtnIntermedio.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                rbtnIntermedioActionPerformed(evt);
            }
        });

        rbtnAvanzado.setText("Nivel Avanzado");
        rbtnAvanzado.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                rbtnAvanzadoActionPerformed(evt);
            }
        });

        lblGanador.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        lblGanador.setForeground(new java.awt.Color(255, 0, 0));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(533, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(lblGanador, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 151, Short.MAX_VALUE)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addComponent(rbtnAvanzado)
                        .addComponent(rbtnIntermedio)
                        .addComponent(rbtnPrincipiante)
                        .addComponent(btnIniciarJuego, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(lblTitleJugador1, javax.swing.GroupLayout.DEFAULT_SIZE, 151, Short.MAX_VALUE)
                        .addComponent(lblJugador1, javax.swing.GroupLayout.DEFAULT_SIZE, 151, Short.MAX_VALUE)
                        .addComponent(lblTitleJugador2, javax.swing.GroupLayout.DEFAULT_SIZE, 151, Short.MAX_VALUE)
                        .addComponent(lblJugador2, javax.swing.GroupLayout.DEFAULT_SIZE, 151, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(lblTitleJugador1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(lblJugador1)
                .addGap(18, 18, 18)
                .addComponent(lblTitleJugador2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(lblJugador2)
                .addGap(18, 18, 18)
                .addComponent(btnIniciarJuego)
                .addGap(18, 18, 18)
                .addComponent(rbtnPrincipiante)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(rbtnIntermedio)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(rbtnAvanzado)
                .addGap(52, 52, 52)
                .addComponent(lblGanador, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(228, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnIniciarJuegoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnIniciarJuegoActionPerformed
        // TODO add your handling code here:
        limpiarTablero();
        turno = 0;
        mm.setProfundidad(turno);
        final_move = false;
    }//GEN-LAST:event_btnIniciarJuegoActionPerformed

    private void rbtnPrincipianteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_rbtnPrincipianteActionPerformed
        // TODO add your handling code here:
        rbtnIntermedio.setSelected(false);
        rbtnAvanzado.setSelected(false);
        profundidad = 2;
    }//GEN-LAST:event_rbtnPrincipianteActionPerformed

    private void rbtnIntermedioActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_rbtnIntermedioActionPerformed
        // TODO add your handling code here:
        rbtnPrincipiante.setSelected(false);
        rbtnAvanzado.setSelected(false);
        profundidad = 5;
    }//GEN-LAST:event_rbtnIntermedioActionPerformed

    private void rbtnAvanzadoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_rbtnAvanzadoActionPerformed
        // TODO add your handling code here:
        rbtnPrincipiante.setSelected(false);
        rbtnIntermedio.setSelected(false);
        profundidad = 9;
    }//GEN-LAST:event_rbtnAvanzadoActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new ventanaPrincipal().setVisible(true);
            }
        });
    }

    private void agregaLabels() {
        lbl1.setBounds(50, 50, 100, 100);
        lbl2.setBounds(200, 50, 100, 100);
        lbl3.setBounds(350, 50, 100, 100);
        lbl4.setBounds(50, 200, 100, 100);
        lbl5.setBounds(200, 200, 100, 100);
        lbl6.setBounds(350, 200, 100, 100);
        lbl7.setBounds(50, 350, 100, 100);
        lbl8.setBounds(200, 350, 100, 100);
        lbl9.setBounds(350, 350, 100, 100);
        lbl1.setIcon(new ImageIcon(getClass().getResource("/practica2_minimax/white.png")));
        lbl2.setIcon(new ImageIcon(getClass().getResource("/practica2_minimax/white.png")));
        lbl3.setIcon(new ImageIcon(getClass().getResource("/practica2_minimax/white.png")));
        lbl4.setIcon(new ImageIcon(getClass().getResource("/practica2_minimax/white.png")));
        lbl5.setIcon(new ImageIcon(getClass().getResource("/practica2_minimax/white.png")));
        lbl6.setIcon(new ImageIcon(getClass().getResource("/practica2_minimax/white.png")));
        lbl7.setIcon(new ImageIcon(getClass().getResource("/practica2_minimax/white.png")));
        lbl8.setIcon(new ImageIcon(getClass().getResource("/practica2_minimax/white.png")));
        lbl9.setIcon(new ImageIcon(getClass().getResource("/practica2_minimax/white.png")));
        agregarListeners();
        panel.add(lbl1);
        panel.add(lbl2);
        panel.add(lbl3);
        panel.add(lbl4);
        panel.add(lbl5);
        panel.add(lbl6);
        panel.add(lbl7);
        panel.add(lbl8);
        panel.add(lbl9);
        panel.repaint();
    }

    public void limpiarTablero()
    {
        posiciones[0] = -1;
        posiciones[1] = -1;
        posiciones[2] = -1;
        posiciones[3] = -1;
        posiciones[4] = -1;
        posiciones[5] = -1;
        posiciones[6] = -1;
        posiciones[7] = -1;
        posiciones[8] = -1;
        lbl1.setIcon(new ImageIcon(getClass().getResource("/practica2_minimax/white.png")));
        lbl2.setIcon(new ImageIcon(getClass().getResource("/practica2_minimax/white.png")));
        lbl3.setIcon(new ImageIcon(getClass().getResource("/practica2_minimax/white.png")));
        lbl4.setIcon(new ImageIcon(getClass().getResource("/practica2_minimax/white.png")));
        lbl5.setIcon(new ImageIcon(getClass().getResource("/practica2_minimax/white.png")));
        lbl6.setIcon(new ImageIcon(getClass().getResource("/practica2_minimax/white.png")));
        lbl7.setIcon(new ImageIcon(getClass().getResource("/practica2_minimax/white.png")));
        lbl8.setIcon(new ImageIcon(getClass().getResource("/practica2_minimax/white.png")));
        lbl9.setIcon(new ImageIcon(getClass().getResource("/practica2_minimax/white.png")));
        lblGanador.setText("");
        panel.repaint();
    }
     private void agregarListeners()
    {
         lbl1.addMouseListener(new java.awt.event.MouseAdapter() {
            @Override
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                lbl1MouseClicked(evt);
            }

            private void lbl1MouseClicked(MouseEvent evt) {
                if(posiciones[0] == -1 && (turno != -1 && turno != 2))
                {
                    if(turno == 0)
                    {
                        lbl1.setIcon(new ImageIcon(getClass().getResource("/practica2_minimax/circle.png")));
                        posiciones[0] = 0;
                        turno = 1;
                    }
                    else if(turno == 1)
                    {
                        lbl1.setIcon(new ImageIcon(getClass().getResource("/practica2_minimax/cross.png")));
                        posiciones[0] = 1;
                        turno = 0;
                    }
                    mm.setTablero(posiciones);
                    posiciones = mm.jugarPC(turno);
                    mm.setTablero(posiciones);
                    establecerGanador();
                    ponerLabelPC();
                }
            }
        });

        lbl2.addMouseListener(new java.awt.event.MouseAdapter() {
            @Override
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                lbl2MouseClicked(evt);
            }

            private void lbl2MouseClicked(MouseEvent evt) {
                if(posiciones[1] == -1 && (turno != -1 && turno != 2))
                {
                    if(turno == 0)
                    {
                        lbl2.setIcon(new ImageIcon(getClass().getResource("/practica2_minimax/circle.png")));
                        posiciones[1] = 0;
                        turno = 1;
                    }
                    else if(turno == 1)
                    {
                        lbl2.setIcon(new ImageIcon(getClass().getResource("/practica2_minimax/cross.png")));
                        posiciones[1] = 1;
                        turno = 0;
                    }
                    mm.setTablero(posiciones);
                    posiciones = mm.jugarPC(turno);
                    mm.setTablero(posiciones);
                    establecerGanador();
                    ponerLabelPC();
                }
            }
        });

        lbl3.addMouseListener(new java.awt.event.MouseAdapter() {
            @Override
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                lbl3MouseClicked(evt);
            }

            private void lbl3MouseClicked(MouseEvent evt) {
                if(posiciones[2] == -1 && (turno != -1 && turno != 2))
                {
                    if(turno == 0)
                    {
                        lbl3.setIcon(new ImageIcon(getClass().getResource("/practica2_minimax/circle.png")));
                        posiciones[2] = 0;
                        turno = 1;
                    }
                    else if(turno == 1)
                    {
                        lbl3.setIcon(new ImageIcon(getClass().getResource("/practica2_minimax/cross.png")));
                        posiciones[2] = 1;
                        turno = 0;
                    }
                    mm.setTablero(posiciones);
                    posiciones = mm.jugarPC(turno);
                    mm.setTablero(posiciones);
                    establecerGanador();
                    ponerLabelPC();
                }
            }
        });

        lbl4.addMouseListener(new java.awt.event.MouseAdapter() {
            @Override
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                lbl4MouseClicked(evt);
            }

            private void lbl4MouseClicked(MouseEvent evt) {
                if(posiciones[3] == -1 && (turno != -1 && turno != 2))
                {
                    if(turno == 0)
                    {
                        lbl4.setIcon(new ImageIcon(getClass().getResource("/practica2_minimax/circle.png")));
                        posiciones[3] = 0;
                        turno = 1;
                    }
                    else if(turno == 1)
                    {
                        lbl4.setIcon(new ImageIcon(getClass().getResource("/practica2_minimax/cross.png")));
                        posiciones[3] = 1;
                        turno = 0;
                    }
                    mm.setTablero(posiciones);
                    posiciones = mm.jugarPC(turno);
                    mm.setTablero(posiciones);
                    establecerGanador();
                    ponerLabelPC();
                }
            }
        });

        lbl5.addMouseListener(new java.awt.event.MouseAdapter() {
            @Override
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                lbl5MouseClicked(evt);
            }

            private void lbl5MouseClicked(MouseEvent evt) {
                if(posiciones[4] == -1 && (turno != -1 && turno != 2))
                {
                    if(turno == 0)
                    {
                        lbl5.setIcon(new ImageIcon(getClass().getResource("/practica2_minimax/circle.png")));
                        posiciones[4] = 0;
                        turno = 1;
                    }
                    else if(turno == 1)
                    {
                        lbl5.setIcon(new ImageIcon(getClass().getResource("/practica2_minimax/cross.png")));
                        posiciones[4] = 1;
                        turno = 0;
                    }
                    mm.setTablero(posiciones);
                    posiciones = mm.jugarPC(turno);
                    mm.setTablero(posiciones);
                    establecerGanador();
                    ponerLabelPC();
                }
            }
        });

        lbl6.addMouseListener(new java.awt.event.MouseAdapter() {
            @Override
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                lbl6MouseClicked(evt);
            }

            private void lbl6MouseClicked(MouseEvent evt) {
                if(posiciones[5] == -1 && (turno != -1 && turno != 2))
                {
                    if(turno == 0)
                    {
                        lbl6.setIcon(new ImageIcon(getClass().getResource("/practica2_minimax/circle.png")));
                        posiciones[5] = 0;
                        turno = 1;
                    }
                    else if(turno == 1)
                    {
                        lbl6.setIcon(new ImageIcon(getClass().getResource("/practica2_minimax/cross.png")));
                        posiciones[5] = 1;
                        turno = 0;
                    }
                    mm.setTablero(posiciones);
                    posiciones = mm.jugarPC(turno);
                    mm.setTablero(posiciones);
                    establecerGanador();
                    ponerLabelPC();
                }
            }
        });

        lbl7.addMouseListener(new java.awt.event.MouseAdapter() {
            @Override
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                lbl7MouseClicked(evt);
            }

            private void lbl7MouseClicked(MouseEvent evt) {
                if(posiciones[6] == -1 && (turno != -1 && turno != 2))
                {
                    if(turno == 0)
                    {
                        lbl7.setIcon(new ImageIcon(getClass().getResource("/practica2_minimax/circle.png")));
                        posiciones[6] = 0;
                        turno = 1;
                    }
                    else if(turno == 1)
                    {
                        lbl7.setIcon(new ImageIcon(getClass().getResource("/practica2_minimax/cross.png")));
                        posiciones[6] = 1;
                        turno = 0;
                    }
                    mm.setTablero(posiciones);
                    posiciones = mm.jugarPC(turno);
                    mm.setTablero(posiciones);
                    establecerGanador();
                    ponerLabelPC();
                }
            }
        });

        lbl8.addMouseListener(new java.awt.event.MouseAdapter() {
            @Override
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                lbl8MouseClicked(evt);
            }

            private void lbl8MouseClicked(MouseEvent evt) {
                if(posiciones[7] == -1 && (turno != -1 && turno != 2))
                {
                    if(turno == 0)
                    {
                        lbl8.setIcon(new ImageIcon(getClass().getResource("/practica2_minimax/circle.png")));
                        posiciones[7] = 0;
                        turno = 1;
                    }
                    else if(turno == 1)
                    {
                        lbl8.setIcon(new ImageIcon(getClass().getResource("/practica2_minimax/cross.png")));
                        posiciones[7] = 1;
                        turno = 0;
                    }
                    mm.setTablero(posiciones);
                    posiciones = mm.jugarPC(turno);
                    mm.setTablero(posiciones);
                    establecerGanador();
                    ponerLabelPC();
                }
            }
        });

        lbl9.addMouseListener(new java.awt.event.MouseAdapter() {
            @Override
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                lbl9MouseClicked(evt);
            }

            private void lbl9MouseClicked(MouseEvent evt) {
                if(posiciones[8] == -1 && (turno != -1 && turno != 2))
                {
                    if(turno == 0)
                    {
                        lbl9.setIcon(new ImageIcon(getClass().getResource("/practica2_minimax/circle.png")));
                        posiciones[8] = 0;
                        turno = 1;
                    }
                    else if(turno == 1)
                    {
                        lbl9.setIcon(new ImageIcon(getClass().getResource("/practica2_minimax/cross.png")));
                        posiciones[8] = 1;
                        turno = 0;
                    }
                    mm.setTablero(posiciones);
                    posiciones = mm.jugarPC(turno);
                    mm.setTablero(posiciones);
                    establecerGanador();
                    ponerLabelPC();
                }
            }
        });
     }

     public void ponerLabelPC()
     {
         if(!final_move)
         {
             for(int i = 0; i < posiciones.length; i++){

                 if(posiciones[i] == 0)
                 {
                     put_turn(i, "/practica2_minimax/circle.png");
                 }
                 else if(posiciones[i] == 1){
                     put_turn(i, "/practica2_minimax/cross.png");
                 }
             }
         }
         if(turno == 0)
             turno = 1;
         else if(turno == 1)
             turno = 0;
     }

     private void put_turn(int i, String path){

         if(i == 0)
         {
             lbl1.setIcon(new ImageIcon(getClass().getResource(path)));
         }
         else if(i == 1)
         {
             lbl2.setIcon(new ImageIcon(getClass().getResource(path)));
         }
         else if(i == 2)
         {
             lbl3.setIcon(new ImageIcon(getClass().getResource(path)));
         }
         else if(i == 3)
         {
             lbl4.setIcon(new ImageIcon(getClass().getResource(path)));
         }
         else if(i == 4)
         {
             lbl5.setIcon(new ImageIcon(getClass().getResource(path)));
         }
         else if(i == 5)
         {
             lbl6.setIcon(new ImageIcon(getClass().getResource(path)));
         }
         else if(i == 6)
         {
             lbl7.setIcon(new ImageIcon(getClass().getResource(path)));
         }
         else if(i == 7)
         {
             lbl8.setIcon(new ImageIcon(getClass().getResource(path)));
         }
         else if(i == 8)
         {
             lbl9.setIcon(new ImageIcon(getClass().getResource(path)));
         }
     }

     public void establecerGanador()
    {
         if(mm.determinarGanador() == 0)
         {
             lblGanador.setText("Ganador -> O");
             turno = 2;
             final_move = true;
         }
         else if(mm.determinarGanador() == 1)
         {
             lblGanador.setText("Ganador -> X");
             turno = 2;
             ponerLabelPC();
             final_move = true;
         }
     }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnIniciarJuego;
    private javax.swing.JLabel lblGanador;
    private javax.swing.JLabel lblJugador1;
    private javax.swing.JLabel lblJugador2;
    private javax.swing.JLabel lblTitleJugador1;
    private javax.swing.JLabel lblTitleJugador2;
    private javax.swing.JRadioButton rbtnAvanzado;
    private javax.swing.JRadioButton rbtnIntermedio;
    private javax.swing.JRadioButton rbtnPrincipiante;
    // End of variables declaration//GEN-END:variables

}
